package com.mycompany.controller;

import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.mycompany.entity.Rule;
import com.mycompany.service.RuleService;
import com.mycompany.service.UserService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class RuleController {
	
	private static final Logger logger = LoggerFactory.getLogger(RuleController.class);
	
	@Autowired
	RuleService ruleService;  
	
	@Autowired
	UserService userService;
	
	@RequestMapping(value = "/admin/rule", method = RequestMethod.GET)
	public String ruleListHandle(Model model) {
		logger.debug("Rule List here!");
		List<Rule> rules = ruleService.findAll();
		model.addAttribute("rules", rules);
		return "admin/rule/rule_list";
	}
	@RequestMapping(value = "/admin/ruleSearch")
	public String ruleFindHandle(Model model,@RequestParam(value = "name") String name) {
		logger.debug("ruleFindHandle here!");
		Rule u = new Rule();
		u.setName(name);
		List<Rule> rules = ruleService.findListByCodition(u);
		model.addAttribute("rules", rules);
		return "admin/rule/rule_list";
	}
	@RequestMapping(value = "/admin/ruleDelete")
	public String ruleDeleteHandle(Model model,@RequestParam(value = "id") int id) {
		logger.debug("ruleDeleteHandle here!");
		Rule u = new Rule();
		u.setRuleId(id);
		u  = ruleService.findById(id);
		ruleService.remove(u);
		List<Rule> rules = ruleService.findAll();
		model.addAttribute("rules", rules);
		return "admin/rule/rule_list";
	}
	
	@RequestMapping(value = "/admin/ruleEditForm")
	public String ruleEditFormHandle(Model model,@RequestParam(value = "id") int id) {
		logger.debug("ruleEditFormHandle here!");
		Rule u = new Rule();
		u.setRuleId(id);
		u  = ruleService.findById(id);
		model.addAttribute("rule", u);
		model.addAttribute("action","ruleEdit");
		return "admin/rule/rule_form";
	}
	
	@RequestMapping(value = "/admin/ruleEdit")
	public String ruleEditHandle(Model model,@ModelAttribute( "Rule" ) @Valid Rule rule) {
		logger.debug("ruleEditHandle here!");
		
		// set properties 
		Rule u = new Rule();
		u  = ruleService.findById(rule.getRuleId());
		u.setBudget(rule.getBudget());
		u.setDetail(rule.getDetail());
		u.setName(rule.getName());
		rule.setActiveFlg(true);
		//u.setPercentRate(rule.getPercentRate());

		// set User
//		UserDetails userDetails = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//		User user = new User();
//		user.setLogin(userDetails.getUsername());
//		user = userService.findListByCodition(user).get(0);
//		u.setUser(user);
		
		ruleService.merge(u);
		u = new Rule();
		u.setRuleId(rule.getRuleId());
		List<Rule> rules = ruleService.findAll();
		model.addAttribute("rules", rules);
		return "admin/rule/rule_list";
	}
	
	
	@RequestMapping(value = "/admin/ruleAddForm")
	public String ruleAddFormHandle(Model model) {
		logger.debug("ruleAddFormHandle here!");
		List<Rule> rules = ruleService.findAll();
		model.addAttribute("action","ruleAdd");
		model.addAttribute("rules",rules);
		return "admin/rule/rule_form";
	}
	
	@RequestMapping(value = "/admin/ruleAdd")
	public String ruleAddHandle(Model model,@ModelAttribute( "Rule" ) @Valid Rule rule,@RequestParam(value = "ruleId") int ruleId) {
		logger.debug("ruleEditHandle here!");
		rule.setActiveFlg(true);
		
		// set Rule
		Rule r =ruleService.findById(ruleId);
		
		// set User
//		UserDetails userDetails = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//		User user = new User();
//		user.setLogin(userDetails.getUsername());
//		user = userService.findListByCodition(user).get(0);
//		r.setUser(user);
//		
		ruleService.persist(r);
		List<Rule> rules = ruleService.findAll();
		model.addAttribute("rules", rules);
		return "admin/rule/rule_list";
	}
}
